package com.atguigu.service.impl;

import com.atguigu.entity.BaseBrand;
import com.atguigu.exception.SleepUtils;
import com.atguigu.mapper.BaseBrandMapper;
import com.atguigu.service.BaseBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author hachimen
 * @since 2021-08-24
 */
@Service
public class BaseBrandServiceImpl extends ServiceImpl<BaseBrandMapper, BaseBrand> implements BaseBrandService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedissonClient redissonClient;
    //@Override
    public synchronized void setNum1() {
        //1.设置锁√
        //2.设置定时锁√
        //3.设置线程唯一锁√
        //4.设置原子性的线程唯一锁√
        String uuid = UUID.randomUUID().toString();
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("lock", uuid, 3, TimeUnit.SECONDS);
        if(aBoolean){//拿到锁

            //业务逻辑
            String num = (String)redisTemplate.opsForValue().get("num");
            if(StringUtils.isEmpty(num)){
                redisTemplate.opsForValue().set("num","1");
                return;
            }
            int i = Integer.parseInt(num);
            redisTemplate.opsForValue().set("num",String.valueOf(++i));

            //if(uuid.equals(redisTemplate.opsForValue().get("lock"))){
            //    redisTemplate.delete("lock");//释放锁
            //}


            String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
            DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
            redisScript.setScriptText(script);
            redisScript.setResultType(Long.class);
            redisTemplate.execute(redisScript, Arrays.asList("lock"),uuid);


        }else{

            SleepUtils.sleepMillis(50);
            setNum();

        }


    }

    @Override
    public void setNum() {
        String uuid = UUID.randomUUID().toString();
        RLock lock = redissonClient.getLock("lock");
        //lock.lock(3,TimeUnit.SECONDS);//设置锁有效时间
        try {
            lock.tryLock(10,3,TimeUnit.SECONDS);


            //业务逻辑
            String num = (String)redisTemplate.opsForValue().get("num");
            if(StringUtils.isEmpty(num)){
                redisTemplate.opsForValue().set("num","1");
                return;
            }
            int i = Integer.parseInt(num);
            redisTemplate.opsForValue().set("num",String.valueOf(++i));


            lock.unlock();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
